package {{.ModuleName}}impl

import (
    "context"
	"testing"
	"time"

	"github.com/stretchr/testify/require"

	"go-caipu/pkg/services/{{.PackageName}}/{{.ModuleName}}"
    "go-caipu/pkg/services/sqlstore"
)

func TestIntegration{{.ClassName}}DataAccess(t *testing.T) {
	t.Helper()

	t.Run("Testing {{.ClassName}} Data Access", func(t *testing.T) {
		ss := sqlstore.InitTestDB(t)
		{{.ModuleName}}Store := ProvideStore(ss)

		t.Run("insert  {{.ClassName}}", func(t *testing.T) {
			cmd := {{.ModuleName}}.{{.ClassName}}{
			    {{- range .Columns -}}
                {{- $z := .IsRequired -}}
                {{- if ($z) -}}
                {{- if eq .GoType "time.Time" -}} {{.GoField}}: time.Now(), {{- end -}}
                {{- if eq .GoType "int64" -}} {{.GoField}} : 1, {{- end -}}
                {{- if eq .GoType "string" -}} {{.GoField}} : "{{.GoField}}_InsertTest",{{- end -}}
                {{- if eq .GoType "int" -}} {{.GoField}} : 1,{{- end -}}
                {{- end -}}
                {{- end -}}
			}
			cmd.CreatedAt = time.Now()
			cmd.UpdatedAt = time.Now()
			err := {{.ModuleName}}Store.Insert(context.Background(),cmd)
			require.NoError(t, err)

			t.Run("Get{{.ClassName}}Page", func(t *testing.T) {
				cmd := {{.ModuleName}}.Get{{.ClassName}}PageCommand{}
				result, count, err := {{.ModuleName}}Store.Get{{.ClassName}}Page(context.Background(),cmd)
				require.NoError(t, err)
				require.Equal(t, 1, len(result))
				require.Equal(t, int64(1), count)
			})
			//
			t.Run("Get{{.ClassName}}", func(t *testing.T) {
				cmdGet := {{.ModuleName}}.Get{{.ClassName}}Command{}
				_, err := {{.ModuleName}}Store.Get(context.Background(),cmdGet)
				require.Error(t, err)
				cmdGet.{{.PkGoField}} = 1
				result, err := {{.ModuleName}}Store.Get(context.Background(),cmdGet)
				require.NoError(t, err)
				require.Equal(t, 1, result.{{.PkGoField}})
			})

			//update
			t.Run("Update {{.ClassName}}", func(t *testing.T) {
				cmdUpdate := {{.ModuleName}}.{{.ClassName}}{}
				//err := {{.ModuleName}}Store.Update(context.Background(),cmdUpdate)
				//require.Error(t, err)
				// cmdUpdate.{{.PkGoField}} = 1
                {{ range .Columns -}}
                {{- $z := .IsRequired -}}
                {{- if ($z) -}}
                {{- if eq .GoType "time.Time" -}} cmdUpdate.{{.GoField}}= time.Now() {{ end -}}
                {{- if eq .GoType "int64" -}} cmdUpdate.{{.GoField}} = 1 {{ end -}}
                {{- if eq .GoType "string" -}} cmdUpdate.{{.GoField}} = "{{.GoField}}_InsertTest" {{ end -}}
                {{- if eq .GoType "int" -}} cmdUpdate.{{.GoField}} = 1 {{ end -}}
                {{- end -}}
                {{- end -}}

				err := {{.ModuleName}}Store.Update(context.Background(),cmdUpdate)
				require.NoError(t, err)
			})
			//remove
			t.Run("remove {{.ClassName}}", func(t *testing.T) {
				cmdDel := {{.ModuleName}}.Remove{{.ClassName}}Command{}
				err := {{.ModuleName}}Store.Remove(context.Background(),cmdDel)
				require.Error(t, err)
				cmdDel.Ids = []int64{2}
				err = {{.ModuleName}}Store.Remove(context.Background(),cmdDel)
				require.NoError(t, err)
			})

		})
	})
}
